# encoding: utf-8
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :lastname, :address, :phone, :cultural_institution, :state, :username, :role, :created_at, :updated_at

  belongs_to :obj_cultural_institution, :class_name => 'CulturalHeritage::CulturalInstitution',  :foreign_key => 'cultural_institution'
  belongs_to :obj_role, :class_name => 'Security::UserRole',  :foreign_key => 'role'

  

  default_scope where("deleted = 0")

  def active_for_authentication?
    super && (state == 1)
  end

  def inactive_message
    if (state != 1)
      "Usuario no permitido."
    else 
      super # Use whatever other message
    end
  end

   def str_state
    if state == 0
      return "Registrado"
    end
    if state == 1
      return "Aprobado"
    end
    if state == 2
      return "Rechazado"
    end
    if state == 3
      return "Suspendido"
    end
  end

   def str_complete_name
     return ''+name+' '+lastname+''
   end


  def self.count_user_role_dependencies(id)
      return find(:all,:conditions=>["users.role = ? and (users.deleted = ?) ",id,0],:select=>"count(*) as 'users'")[0].users.to_i
  end

  def self.count_culturalInstitution_dependencies(id)
      return find(:all,:conditions=>["users.cultural_institution  = ?  and (users.deleted = ?) ",id,0],:select=>"count(*) as 'users'")[0].users.to_i
  end

  def erasable
    comp = RequestAndComplaint::Complaint.count_user_dependencies(id)
    cult_file = CulturalHeritage::CulturalHeritageFile.count_user_dependencies(id)
    cult_video = CulturalHeritage::CulturalHeritageYoutube.count_user_dependencies(id)
#    inci = CulturalHeritage::Incidence.count_user_dependencies(id)
    mod3d = CulturalHeritage::Model3D.count_user_dependencies(id)
    reg_req = RequestAndComplaint::RegistrationRequest.count_user_dependencies(id)
    resea = CulturalHeritage::Researcher.count_user_dependencies(id)
    trans = CulturalHeritage::Transfer.count_user_dependencies(id)
    if comp >0
      return "Imposible borrar. Existen dependencias en denuncia"
    end
    if cult_file >0
      return "Imposible borrar. Existen dependencias en investigacion de patrimonio cultural"
    end
    if cult_video >0
      return "Imposible borrar. Existen dependencias en video de patrimonio cultural"
    end
#    if inci >0
#      return "Imposible borrar. Existen dependencias en incidencia"
#    end
    if mod3d >0
      return "Imposible borrar. Existen dependencias en modelo 3D de patrimonio cultural"
    end
    if reg_req >0
      return "Imposible borrar. Existen dependencias en solicitud de registro"
    end
    if resea >0
      return "Imposible borrar. Existen dependencias en investigadores"
    end
    if trans >0
      return "Imposible borrar. Existen dependencias en traslados"
    end
    return nil
  end
  
  
  protected
  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    login = conditions.delete(:username)
    where(conditions).where(["lower(username) = :value", { :value => login.downcase }]).first
  end

end
